//
//  TLBottomBar.m
//  Books
//
//  Created by tao故乡的云 on 2017/3/24.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "TLBottomBar.h"

@interface TLBottomBar ()
@property (weak, nonatomic)  UIButton *deleteBtn;
@property (weak, nonatomic)  UIButton *loveBtn;
@property (weak, nonatomic)  UIButton *editBtn;
@property (weak, nonatomic)  UIView *barView;

@end

@implementation TLBottomBar

static id _instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
        [self setup];
    });
    return _instance;
}

+ (instancetype)sharedBottomBar {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
        [self setup];
    });
    return _instance;
}

+ (instancetype)show {
   TLBottomBar *window = [self sharedBottomBar];
    window.hidden = NO;
    return window;
}

+ (void)hidden {
    UIWindow *window = (UIWindow *)_instance;
    [window setHidden:YES];
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

+ (void)setup {
    TLBottomBar *barWindow = (TLBottomBar *)_instance;
    barWindow.backgroundColor = [UIColor clearColor];
    UIScreen *screen = [UIScreen mainScreen];
    barWindow.frame = CGRectMake(0, screen.bounds.size.height - 60, screen.bounds.size.width,60);
    [barWindow makeKeyAndVisible];
}

- (void)setUpSubViews {
    if (_editBtn == nil || _loveBtn == nil || _editBtn == nil) {
        self.barView.hidden = NO;
        self.deleteBtn.enabled = NO;
        self.loveBtn.enabled = YES;
        self.editBtn.enabled = YES;
        [self layoutIfNeeded];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.barView.frame = self.bounds;
    CGFloat x = 0;
    CGFloat w = 44;
    CGFloat h = 44;
    CGFloat y = (self.bounds.size.height - h) / 2.0;
    CGFloat magin = (self.bounds.size.width - self.barView.subviews.count * w) / (self.barView.subviews.count + 1.0);
    NSInteger i = 0;
    for (UIButton *btn in self.barView.subviews) {
        x = magin + i++ * (magin + w);
        btn.frame = CGRectMake(x, y, w, h);
    }
}

#pragma mark - 事件监听
// 删除词条
- (void)deleteItem:(id)sender {
    if ([self.delegate respondsToSelector:@selector(bottomBar:didClickDeleteButton:)]) {
        [self.delegate bottomBar:self didClickDeleteButton:sender];
    }
}

// 收藏
- (void)love:(id)sender {
    if ([self.delegate respondsToSelector:@selector(bottomBar:didClickLoveButton:)]) {
        [self.delegate bottomBar:self didClickLoveButton:sender];
    }
}

// 编辑词条
- (void)edit:(id)sender {
    if ([self.delegate respondsToSelector:@selector(bottomBar:didClickEditButton:)]) {
        [self.delegate bottomBar:self didClickEditButton:sender];
    }
}


#pragma mark - 属性设置
- (void)setDeleteEnabe:(bool)deleteEnabe {
    [self setUpSubViews];
    _deleteEnabe = deleteEnabe;
    self.deleteBtn.enabled = deleteEnabe;
}

- (void)setLoveSelected:(bool)loveSelected {
    _loveSelected = loveSelected;
    [self setUpSubViews];
    if (_loveSelected) {
        [self.loveBtn setImage:[UIImage imageNamed:@"tabbar_compose_friend"] forState:UIControlStateNormal];
    }else {
        [self.loveBtn setImage:[UIImage imageNamed:@"tabbar_compose_unLove"] forState:UIControlStateNormal];
    }
}

#pragma mark - properties
- (UIView *)barView {
    if (_barView == nil) {
        UIView *barView = [[UIView alloc] initWithFrame:self.bounds];
        _barView = barView;
        barView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:barView];
    }
    
    return _barView;
}


- (UIButton *)deleteBtn {
    if (_deleteBtn == nil) {
        UIButton *deleteBtn = [[UIButton alloc] init];
        _deleteBtn = deleteBtn;
        [deleteBtn addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
        [self.barView addSubview:deleteBtn];
        [deleteBtn setImage:[UIImage imageNamed:@"tabbar_compose_delete"] forState:UIControlStateNormal];
        [deleteBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
    }
    
    return _deleteBtn;
}

- (UIButton *)loveBtn {
    if (_loveBtn == nil) {
        UIButton *loveBtn = [[UIButton alloc] init];
        _loveBtn = loveBtn;
        [self.barView addSubview:loveBtn];
        [loveBtn addTarget:self action:@selector(love:) forControlEvents:UIControlEventTouchUpInside];
        [loveBtn setImage:[UIImage imageNamed:@"tabbar_compose_unLove"] forState:UIControlStateNormal];
//        [loveBtn setImage:[UIImage imageNamed:@"tabbar_compose_friend"] forState:UIControlStateNormal];
    }
    
    return _loveBtn;
}

- (UIButton *)editBtn {
    if (_editBtn == nil) {
        UIButton *editBtn = [[UIButton alloc] init];
        _editBtn = editBtn;
        [self.barView addSubview:editBtn];
        [editBtn addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
        [editBtn setImage:[UIImage imageNamed:@"tabbar_compose_weibo"] forState:UIControlStateNormal];
    }
    
    return _editBtn;
}

@end
